package com.zcl.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zcl.dao.Accident;
import com.zcl.service.AccidentService;
import com.zcl.service.LogService;
import com.zcl.util.GetCurrentUserDetails;
import com.zcl.util.UserOperation;

@Controller
public class AccidentController {
	@Autowired
	AccidentService accidentService;
	@Autowired
	LogService logService;
	/**
	 * 指向事故页面
	 * @return
	 */
	@RequestMapping("/accident")
	public String accidentIndex() {
		return "accident";
	}
	
	/**
	 * 事故信息概况查询
	 */
	@ResponseBody
	@RequestMapping("/accident/surveryQuery")
	public List<Map<String, String>> accidentQuery(Model model) {
		List<Map<String, String>> accidentList = accidentService.accidentQuery();
		return accidentList;
	}
	
	/**
	 * 事故信息概况-操作-详情
	 * @return
	 */
	@RequestMapping("/accidentSurveryInfo")
	public String accidentSuveryInfo(@RequestParam("accidentId") String accidentId,Model model) {
		Map<String, String> map = new HashMap<String,String>();
		map.put("accidentId", accidentId);
		Accident accident = accidentService.accidentSuveryInfo(map);
		if (accident.getAccidentKind().equals("0")) {
			accident.setAccidentKind("碰撞事故");
		}else if (accident.getAccidentKind().equals("1")) {
			accident.setAccidentKind("搁浅事故");
		}else if (accident.getAccidentKind().equals("2")) {
			accident.setAccidentKind("触礁事故");
		}else if (accident.getAccidentKind().equals("3")) {
			accident.setAccidentKind("触损事故");
		}else if (accident.getAccidentKind().equals("4")) {
			accident.setAccidentKind("浪损事故");
		}else if (accident.getAccidentKind().equals("5")) {
			accident.setAccidentKind("火灾、爆炸事故");
		}else if (accident.getAccidentKind().equals("6")) {
			accident.setAccidentKind("风灾事故");
		}else if (accident.getAccidentKind().equals("7")) {
			accident.setAccidentKind("自沉事故");
		}else if (accident.getAccidentKind().equals("8")) {
			accident.setAccidentKind("其他事故");
		}
		model.addAttribute("accident", accident);
		System.out.println("事故信息概况-操作-详情:accidentId="+accidentId);
		return "accident_information";
	}
	
	/**
	 * 事故信息概况-操作-编辑
	 * @return
	 */
	@RequestMapping("/accidentSurveryRevise")
	public String accidentSuveryRevise(@RequestParam("accidentId") String accidentId,Model model) {
		Map<String, String> map = new HashMap<String,String>();
		map.put("accidentId", accidentId);
		Accident accident = accidentService.accidentSuveryInfo(map);
		model.addAttribute("accident", accident);
		System.out.println("事故信息概况-操作-编辑:accidentId="+accidentId);
		
		return "accident_revise";
	}
	
	/**
	 * 事故信息概况-操作-编辑2--获取某些下拉框值
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/accidentSurveryRevise2")
	public Accident accidentSuveryRevise2(@RequestParam("accidentId") String accidentId,Model model) {
		Map<String, String> map = new HashMap<String,String>();
		map.put("accidentId", accidentId);
		Accident accident = accidentService.accidentSuveryInfo(map);
		return accident;
	}
	
	/**
	 * 事故信息概况-操作-删除
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/accident/delete")
	public void accidentDelete(@RequestParam("accidentId") String accidentId) {
		Map<String, String> map = new HashMap<String,String>();
		map.put("accidentId", accidentId);
		accidentService.accidentDelete(map);
		logService.operationRecord(UserOperation.operationRecord("事故信息删除"));
		System.out.println("删除");;
	}
	
	/**
	 * 事故添加页面
	 * @return
	 */
	@RequestMapping("/accidentAdd")
	public String accidentAccidentAdd() {
		return "accident_add";
	}
	
	/**
	 * 事故信息查询-全文搜索
	 * @param search_input
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/accident/globalQuery")
	public List<Map<String, String>> accidentGlobalQuery(@RequestParam("search_input") String search_input){
		Map<String, String> map = new HashMap<String,String>();
		map.put("search_input", search_input);
		List<Map<String, String>> accidentList = accidentService.accidentGlobalQuery(map);
		return accidentList;
		
	} 
	
	/**
	 * 事故信息查询-高级搜索
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/accident/advancedQuery")
	public List<Map<String, String>> accidentAdvancedQuery(HttpServletRequest request){
		Map<String, String> map = new HashMap<String,String>();
		
		map.put("accidentId", request.getParameter("accidentId"));
		map.put("accidentName", request.getParameter("accidentName"));
		map.put("accidentKind", request.getParameter("accidentKind"));
		map.put("accidentWriter", request.getParameter("accidentWriter"));
		map.put("accidentTime", request.getParameter("accidentTime"));
		List<Map<String, String>> accidentList = accidentService.accidentAdvancedQuery(map);
		return accidentList;
		
	} 
	
	/**
	 * 事故信息录入
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("/accident/addAccident")
	public void accidentAdd(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("utf-8");
		String successly = request.getParameter("successly");
		successly = URLDecoder.decode(successly, "UTF-8");
		successly = URLDecoder.decode(successly, "UTF-8");
		String timely = request.getParameter("timely");
		timely = URLDecoder.decode(timely, "UTF-8");
		timely = URLDecoder.decode(timely, "UTF-8");
		String accidentId = UUID.randomUUID().toString();
		Map<String, String> map = new HashMap<String,String>();
		map.put("accidentId", accidentId);
		map.put("accidentTime", request.getParameter("accidentTime"));
		map.put("accidentKind", request.getParameter("accidentKind"));
		map.put("accidentWriter", GetCurrentUserDetails.currentUser().getUsername());
		map.put("accidentName", request.getParameter("accidentName"));
		map.put("accidentType", request.getParameter("accidentType"));
		map.put("boatId", request.getParameter("boatId"));
		map.put("boatName", request.getParameter("boatName"));
		map.put("direction", request.getParameter("direction"));
		map.put("distance", request.getParameter("distance"));
		map.put("file", request.getParameter("file"));
		map.put("info_from", request.getParameter("from"));
		map.put("lat", request.getParameter("lat"));
		map.put("lng", request.getParameter("lng"));
		map.put("successly", successly);
		map.put("temperature", request.getParameter("temperature"));
		map.put("timely", timely);
		map.put("wave", request.getParameter("wave"));
		map.put("wind", request.getParameter("wind"));
		map.put("accident_describe", request.getParameter("describe"));
		System.out.println("accidentAdd");
		logService.operationRecord(UserOperation.operationRecord("事故信息录入"));
		accidentService.accidentAdd(map);
	}
	
	/**
	 * 事故信息修改
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("/accident/reviseAccident")
	public void accidentRevise(HttpServletRequest request) throws UnsupportedEncodingException {
		request.setCharacterEncoding("utf-8");
		String successly = request.getParameter("successly");
		successly = URLDecoder.decode(successly, "UTF-8");
		successly = URLDecoder.decode(successly, "UTF-8");
		String timely = request.getParameter("timely");
		timely = URLDecoder.decode(timely, "UTF-8");
		timely = URLDecoder.decode(timely, "UTF-8");
		Map<String, String> map = new HashMap<String,String>();
		map.put("accidentId", request.getParameter("accident_id"));
		map.put("accidentKind", request.getParameter("accident_kind"));
		map.put("accidentName", request.getParameter("accident_name"));
		map.put("accidentType", request.getParameter("accident_type"));
		map.put("boatId", request.getParameter("boat_id"));
		//map.put("boatName", request.getParameter("boatName"));
		map.put("direction", request.getParameter("direction"));
		map.put("distance", request.getParameter("distance"));
		map.put("file", request.getParameter("file"));
		map.put("info_from", request.getParameter("from"));
		map.put("lat", request.getParameter("lat"));
		map.put("lng", request.getParameter("lng"));
		map.put("successly", successly);
		map.put("temperature", request.getParameter("temperature"));
		map.put("timely", timely);
		map.put("wave", request.getParameter("wave"));
		map.put("wind", request.getParameter("wind"));
		map.put("accident_describe", request.getParameter("describe"));
		System.out.println("accidentRevise");
		logService.operationRecord(UserOperation.operationRecord("事故信息修改"));
		accidentService.accidentRevise(map);
	}
	
	/**
	 * 事故发生月份统计
	 * @param map
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/accident/echartCount")
	public List<Map<String, String>> accidentEchartCount(@RequestParam("year") String year){
		Map<String, String> map = new HashMap<String,String>();
		map.put("year", year);
		List<Map<String, String>> countList= accidentService.accidentCount(map);
		return countList;
	}
	
	/**
	 * 事故发生类型
	 * @param map
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/accident/echartKind")
	public List<Map<String, String>> accidentEchartKind(@RequestParam("year") String year){
		Map<String, String> map = new HashMap<String,String>();
		map.put("year", year);
		List<Map<String, String>> kindList= accidentService.accidentKind(map);
		return kindList;
	}
	
}
